package com.taxi.driver.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.mikepenz.iconics.IconicsDrawable;
import com.taxi.driver.R;

/**
 * 功能描述：
 */
public class ImageViewPlus extends android.support.v7.widget.AppCompatImageView {

    //获取对应的属性值 Android框架自带的属性 attr
    int pressed = android.R.attr.state_pressed;
    int selected = android.R.attr.state_selected;

    public ImageViewPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public ImageViewPlus(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        // TypeArray中含有我们需要使用的参数
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconPlus, defStyle, 0);
        if (a == null) return;
        Drawable drawable = getDrawable();
        String icon = a.getString(R.styleable.IconPlus_icon_normal);
        int size = a.getDimensionPixelSize(R.styleable.IconPlus_size_normal, -1);
        if (!TextUtils.isEmpty(icon) && size != -1) {
            int color = a.getColor(R.styleable.IconPlus_color_normal, 0);
            int colorPressed = a.getColor(R.styleable.IconPlus_color_pressed, 0);
            int colorSelected = a.getColor(R.styleable.IconPlus_color_selected, 0);
            String iconSelected = a.getString(R.styleable.IconPlus_icon_selected);
            drawable = getDrawable(context, icon, color, size, colorPressed, colorSelected, iconSelected);
        }
        setImageDrawable(drawable);
    }

    private Drawable getDrawable(Context context, String icon, int color, int size, int colorPressed, int colorSelected, String iconSelected) {
        //创建StateListDrawable
        StateListDrawable stateListDrawable = new StateListDrawable();

        //获取选中时显示的drawable
        if (!TextUtils.isEmpty(iconSelected)) {
            IconicsDrawable drawableSelected = new IconicsDrawable(context, iconSelected); //使用iconSelected
            int clr = colorSelected == 0 ? color : colorSelected;
            drawableSelected.color(clr);
            drawableSelected.sizePx(size);
            stateListDrawable.addState(new int[]{selected}, drawableSelected);
        } else if (colorSelected != 0) {
            IconicsDrawable drawableSelected = new IconicsDrawable(context, icon); //使用icon
            drawableSelected.color(colorSelected);
            drawableSelected.sizePx(size);
            stateListDrawable.addState(new int[]{selected}, drawableSelected);
        }

        //获取点击时显示的drawable
        if (colorPressed != 0) {
            IconicsDrawable drawablePressed = new IconicsDrawable(context, icon); //使用icon
            drawablePressed.color(colorPressed);
            if (size != -1) drawablePressed.sizePx(size);
            stateListDrawable.addState(new int[]{pressed}, drawablePressed);
        }

        //获取默认显示的drawable
        IconicsDrawable drawable = new IconicsDrawable(context, icon);
        if (color != 0) drawable.color(color);
        if (size != -1) drawable.sizePx(size);
        stateListDrawable.addState(new int[]{}, drawable);
        return stateListDrawable;
    }

}
